package cur.nature.framework.core.util;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class CollectUtil {

    /**
     * 以newList为准, 当newList存在，oldList不存在时,增加; 当newList不存在, oldList存在时, 删除;
     * @param newList
     * @param oldList
     * @param addList
     * @param deleteList
     */
    public static void compareCollect(final List<Integer> newList, final List<Integer> oldList,
            final List<Integer> addList, final List<Integer> deleteList){
        //list 为空时，返回
        if(addList == null || deleteList == null){
            return;
        }
        // 当newList为空，oldList不为空时, 返回
        if(ObjectUtil.isEmpty(newList) && ObjectUtil.isEmpty(oldList)){
           return;
        }
        // 当newList为空，oldList不为空时, 删除
        if(ObjectUtil.isEmpty(newList) && ObjectUtil.isNotEmpty(oldList)){
            deleteList.addAll(oldList);
            return;
        }
        // 当newList不为空，oldList为空时, 新增
        if(ObjectUtil.isNotEmpty(newList) && ObjectUtil.isEmpty(oldList)){
            addList.addAll(newList);
            return;
        }
        // 当newList存在，oldList不存在时,新增;
        for (Integer newId : newList) {
            boolean present = oldList.stream().filter(s -> s.compareTo(newId) == 0).findFirst().isPresent();
            if(!present){
                addList.add(newId);
            }
        }
        // 当newList不存在, oldList存在时, 删除;
        for (Integer oldId : oldList) {
            boolean present = newList.stream().filter(s -> s.compareTo(oldId) == 0).findFirst().isPresent();
            if(!present){
                deleteList.add(oldId);
            }
        }
    }

    /**
     * 删除集合里的某一个值
     * @param list
     * @param element
     */
    public static void remveElement(List<Integer> list, Integer element) {
        if (ObjectUtil.isEmpty(list) || element == null){return;}
        Iterator<Integer> iterator = list.iterator();
        while (iterator.hasNext()){
            Integer data = iterator.next();
            if(data.compareTo(element) == 0){
                iterator.remove();
            }
        }
    }

    /**
     * 以 / 为分隔, 找出所有父类路径, /system/role --> /system
     * @param childrenPath
     * @param resultSet
     */
    public static void findParentPath(Set<String> childrenPath, Set<String> resultSet){
        if(ObjectUtil.isNotEmpty(childrenPath)){
            for (String childPath : childrenPath) {
                String path = childPath;
                boolean isEnd = false;
                while (!isEnd){
                    int lastIndex = path.lastIndexOf("/");
                    if(lastIndex != -1 && lastIndex != 0){
                        path = path.substring(0, lastIndex);
                        resultSet.add(path);
                    }else {
                        isEnd = true;
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
//        Set<String> parentSet = new HashSet<>();
//        Set<String> resultSet = new HashSet<>();
//        parentSet.add("/system/role");
//        parentSet.add("/system/log");
//        parentSet.add("/system/log/log1");
//        parentSet.add("/system/log/log1/log2");
//        findParentPath(parentSet,resultSet);
//        System.out.println(JsonUtil.toJSONString(resultSet));

        System.out.println((CollectUtil.class == CollectUtil.class));
    }

    public static boolean isNotEmpty(Collection remindList) {
        if (remindList != null && !remindList.isEmpty()) {
            return true;
        }
        return false;
    }

    public static boolean isEmpty(Collection remindList) {
        if (remindList == null || remindList.isEmpty()) {
            return true;
        }
        return false;
    }
}
